package com.itheima.tlias.aspect;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 统计接口耗时切面
 */
@Slf4j
@Aspect
@Component
public class CountTimeAspect {

    @Resource
    HttpServletRequest request;


    @Around("execution(* com.itheima.tlias.service.impl.*.*(..))")
    public Object count(ProceedingJoinPoint point){
        String token = request.getHeader("token");
        log.info("获取token{}",token);
        if(Objects.nonNull(token)) {
            JSONObject jsonObject = JwtUtil.verifyToken(token);
            log.info("操作人{}", jsonObject);
        }
        //获取参数
        Object[] args = point.getArgs();
        log.info("请求参数{}",args);
        //获取类名
        Object target = point.getTarget();
        log.info("获取类名{}",target);
        long start = System.currentTimeMillis();
        //执行父类方法
        try {
            Object proceed = point.proceed();
            log.info("获取返回值{}",proceed);
            long end = System.currentTimeMillis() - start;
            log.info(point.getSignature().getName()+"{}执行时间为:{}",point.getSignature().getName(),end);
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
